package com.appheader.rongmaphone.utils.network;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.appheader.rongmaphone.cache.file.ResourceFileManager;
import com.appheader.rongmaphone.constants.GlobalVars;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUtils {
    public static final String IMAGE_PATH = GlobalVars.getPackageInfo().packageName + File.separator + "download"
            + File.separator + "public_resource" + File.separator + "files" + File.separator + "image";
    //sd卡的根目录

    private static String mSdRootPath = Environment.getExternalStorageDirectory() + File.separator;

    private static String mDataRootPath = null;

    //手机的缓存根目录
    //保存Image的目录名
//    private static final String PATH =mSdRootPath+ IMAGE_PATH + File.separator;

    private static String PATH = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator
            + GlobalVars.getPackageInfo().packageName
            + "/download"
            + File.separator + "public_resource/files/image";

    private final static String FOLDER_NAME = "/m/images";

    public FileUtils(Context context) {
        mDataRootPath = context.getCacheDir().getPath();
    }
    //获取储存Image的目录

    public static String getStorageDirectory() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ? mSdRootPath + IMAGE_PATH : mDataRootPath + FOLDER_NAME;
    }

    //保存Image的方法，有sd卡存储到sd卡，没有就存储到手机目录

    public void savaBitmap(String url, Bitmap bitmap) throws IOException {
        if (bitmap == null) {
            return;
        }
        String path = getStorageDirectory();
        File folderFile = new File(path);
        if (!folderFile.exists()) {
            folderFile.mkdirs();
        }
        File file = new File(path + File.separator + getFileName(url));
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    }


    // 从手机或者sd卡获取Bitmap

    public static Bitmap getBitmap(String url) {
        Log.e("BITMAP", "getBitmap: " + getStorageDirectory() + File.separator + getImageName(url));
        return BitmapFactory.decodeFile(getStorageDirectory() + File.separator + getImageName(url));
    }

    public static boolean isMounted() {
        //获取sd卡状态
        String state = Environment.getExternalStorageState();

        return state.equals(Environment.MEDIA_MOUNTED);
    }

    public static Bitmap redImg(String imagePath) {

        if (!isMounted()) {

            return null;
        }

        File file = new File(PATH, getImageName(imagePath));

        if (file.exists()) {

            return BitmapFactory.decodeFile(PATH + File.separator + getImageName(imagePath));
        }

        return null;
    }

    public static String getImageName(String url) {

        if (!TextUtils.isEmpty(url)) {
            return ResourceFileManager.splitImgUrlPath(url).substring(
                    ResourceFileManager.splitImgUrlPath(url).lastIndexOf("/") + 1);
        }
        return null;
    }


    // 判断文件是否存在

    public boolean isFileExists(String fileName) {
        return new File(getStorageDirectory() + File.separator + getFileName(fileName)).exists();
    }

    // 获取文件的大小

    public long getFileSize(String url) {
        return new File(getStorageDirectory() + File.separator + getFileName(url)).length();
    }


    //删除SD卡或者手机的缓存图片和目录

    public void deleteFile() {
        File dirFile = new File(getStorageDirectory());
        if (!dirFile.exists()) {
            return;
        }
        if (dirFile.isDirectory()) {
            String[] children = dirFile.list();
            for (int i = 0; i < children.length; i++) {
                new File(dirFile, children[i]).delete();
            }
        }

        dirFile.delete();
    }

    public static String getFileName(String url) {
        return url.substring(url.lastIndexOf("/") + 1);
    }

}